const baseURL = 'https://fxg.jinritemai.com/'
const getApiUrl = {
    // getCoupon: 'marketing/coupons/v1/list',
    // getcouponsD: 'marketing/coupons/v1/shopcoupons/',
    getActPrdList: 'marketing/activity/v1/query_available_product_v2',
    checkList: 'marketing/promotion/v1/check_promotion_validation'

}
const ECCOMMARKETSHOP = "ecom_market_shop"

onmessage = function (e) {//监听主进程的消息
    switch (e.data.type) {
        case 'getActPrdList':
            getlist(e.data.data)
            break;
        default:
            break;
    }
}
let getCouponListData = []
let proterData = []
let inter = 1
function getlist(data, isket = false) {
    console.log('进入的消息', data)
    return new Promise((resolve, reject) => {
        getAllActPrdList()

    })
}

function getAllActPrdList() {
    const upData = {
        // page: 1,
        // page_size: 100,
        _bid: ECCOMMARKETSHOP
    }
    const url = baseURL + getApiUrl.getActPrdList + splitJoint(upData)
    fetchWithTimeout(url, {
        method: 'POST',
        body: JSON.stringify({
            "page": 1,
            "pageSize": 100,
            "activityToolType": 26,
            // "product_id": "3684570914181218576",
            "business_code": "",
            "start_time": '',
            "end_time": '',
            "limit_stock_type": 0,
            "config_scope": 0
        }),
        headers: {
            "Content-Type": "application/json",
        },
        mode: 'cors',
        // credentials: 'include',
        timeout: 4000
    }).then(response => {
        if (response.ok) {
            response.json().then(data => {
                // console.log('请求的结果数据', data)
                if (data.data.length) {
                    console.log('datasss', data)
                    // getCouponList(data, true)
                    postMessage({
                        type: 'putActPrdList',
                        data: data
                    })
                } else {
                    console.log('请求的结果数据', data)
                }
            })
        }
    })
}

const fetchWithTimeout = (resource, options = {}) => {
    const { timeout = 8000 } = options; // 设置默认超时时间为8000ms

    const controller = new AbortController();
    const id = setTimeout(() => controller.abort(), timeout);

    const response = fetch(resource, {
        ...options,
        signal: controller.signal
    }).then((response) => {
        clearTimeout(id);
        return response;
    }).catch((error) => {
        clearTimeout(id);
        throw error;
    });

    return response;
}

function splitJoint(obj) {
    let arr = []
    for (let key in obj) {
        arr.push(key + "=" + obj[key])
    }
    return arr.length > 0 ? "?" + arr.join("&") : ""
}